/*************************************************************************
	> File Name: FlyWeight.cc
	> Author: Joker
	> Mail: mahongtao@foxmail.com 
	> Created Time: 2020-09-22 13:55:16
 ************************************************************************/

#include "FlyWeight.h"

#include <iostream>

using std::cout;
using std::endl;

/**************************
 *FlyWeight
 *
 * ***********************/
FlyWeight::FlyWeight(const STATE & state)
    :_State(state)
{
    cout << "FlyWeight(const STATE &)" << endl;
}

FlyWeight::~FlyWeight(){
    cout << "~FlyWeight()" << endl;
}

inline STATE FlyWeight::GetIntrinsicState(){
    return _State;
}

/**************************
 *
 *
 * ***********************/
FlyWeightFactory::FlyWeightFactory(){
    cout << "FlyWeightFactory()" << endl;
}

FlyWeightFactory::~FlyWeightFactory(){
    list<FlyWeight *>::iterator iter;
    for(iter = _listFlyWeight.begin(); iter != _listFlyWeight.end(); ++iter){
        delete *iter;
    }

    _listFlyWeight.clear();
}

FlyWeight * FlyWeightFactory::GetFlyWeight(const STATE & key){
    list<FlyWeight *>::iterator iter;
    for(iter = _listFlyWeight.begin(); iter != _listFlyWeight.end(); ++iter){
        if(key == (*iter)->GetIntrinsicState()){
            cout << "The FlyWeight : " << key << " already exits" << endl;
            return (*iter);
        }
    }

    cout << "Creating a new FlyWeight " << key << endl;
    FlyWeight * flyWeight = new ConcreateFlyWeight(key);
    _listFlyWeight.push_back(flyWeight);
    return flyWeight;
}

/**************************
 *ConcreateFlyWeight
 *
 * ***********************/
ConcreateFlyWeight::ConcreateFlyWeight(const STATE & state)
    :FlyWeight(state)
{
    cout << "ConcreateFlyWeight(const STATE &)" << endl;
}

ConcreateFlyWeight::~ConcreateFlyWeight(){
    cout << "~ConcreateFlyWeight()" << endl;
}

void ConcreateFlyWeight::Operation(STATE & ExtrinsicState){
    cout << "hello word!" << endl
        << ExtrinsicState << endl;
}


